﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BCDataCenter.BOL;

public partial class WebUserControl_InHoaDonBanHang : System.Web.UI.UserControl
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Printed();
    }
    public class MsgBoxEventArgs : System.EventArgs
    {
        public enmAnswer Answer;
        public string Args;

        public MsgBoxEventArgs(enmAnswer answer, string args)
        {
            Answer = answer;
            Args = args;
        }
    }

    public class Message
    {
        public Message(string messageText)
        {
            _messageText = messageText;            
        }

        private string _messageText = "";
        

        public string MessageText
        {
            get { return _messageText; }
            set { _messageText = value; }
        }

    }



    public enum enmAnswer
    {
        OK = 0,
        Cancel = 1
    }
    //public enum enmMessageType
    //{
    //    Error = 0,
    //    Success = 1,
    //    Attention = 2,
    //    Info = 3
    //}

    public delegate void MsgBoxEventHandler(object sender, MsgBoxEventArgs e);
    public event MsgBoxEventHandler MsgBoxAnswered;

    public string Args
    {
        get
        {
            if (ViewState["Args"] == null)
                ViewState["Args"] = "";

            return (Convert.ToString(ViewState["Args"]));
        }
        set
        {
            ViewState["Args"] = value;
        }
    }

    protected int MessageNumber
    {
        get
        {
            return Messages.Count;
        }
    }

    private List<Message> Messages = new List<Message>();

    public void AddMessage(string msgText)
    {
        Messages.Add(new Message(msgText));

        Args = "";
        btnPostCancel.Visible = false;
        btnPostOK.Visible = false;
        btnOK.Visible = true;
    }

    public void AddMessage(string msgText, bool postPage, bool showCancelButton, string args)
    {
        Messages.Add(new Message(msgText));

        if (!string.IsNullOrEmpty(args))
            Args = args;

        btnPostCancel.Visible = showCancelButton;
        btnPostOK.Visible = postPage;
        btnOK.Visible = !postPage;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (btnOK.Visible == false)
            mpeMsg.OkControlID = "btnD2";
        else
            mpeMsg.OkControlID = "btnOK";

        if (Messages.Count > 0)
        {
            btnOK.Focus();           

            mpeMsg.Show();
            udpMsj.Update();
        }
        else
        {
            
            udpMsj.Update();
        }
        if (this.Parent.GetType() == typeof(UpdatePanel))
        {
            UpdatePanel containerUpdatepanel = this.Parent as UpdatePanel;

            containerUpdatepanel.Update();
        }
    }

    protected void btnPostOK_Click(object sender, EventArgs e)
    {
        if (MsgBoxAnswered != null)
        {
            MsgBoxAnswered(this, new MsgBoxEventArgs(enmAnswer.OK, Args));
            Args = "";
        }
    }

    protected void btnPostCancel_Click(object sender, EventArgs e)
    {
        if (MsgBoxAnswered != null)
        {
            MsgBoxAnswered(this, new MsgBoxEventArgs(enmAnswer.Cancel, Args));
            Args = "";
        }
    }
    private void Printed()
    {
        try
        {
            if (Session["MaHDPrinte"] != null)
            {
                BWInCacNghiepVuKho objNVK = new BWInCacNghiepVuKho();
                DataSet ds = new DataSet();
                //Trung Tam PP
                ds = objNVK.GetTrungTamPP(ref error, Session["Sitecode"].ToString());
                lblTrungTamPP.Text = ds.Tables[0].Rows[0]["sitename"].ToString();
                lblDiaChi.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                lblTelFax.Text = ds.Tables[0].Rows[0]["TelFax"].ToString();

                BWNhapHoaDonBanHang obj = new BWNhapHoaDonBanHang();
                DataTable dtHoDon = obj.dsInfoHoaDonChuaIn(ref error, Session["MaHDPrinte"].ToString()).Tables[0];
                lblTenTVV.Text = dtHoDon.Rows[0]["TenTVV"].ToString();
                lblDienThoai.Text = dtHoDon.Rows[0]["DienThoai"].ToString();
                lblSoDonHang.Text = dtHoDon.Rows[0]["MaHD"].ToString();
                lblNgayDonHang.Text = dtHoDon.Rows[0]["NgayHD"].ToString();

                double dTienChuaCK = double.Parse(dtHoDon.Rows[0]["TienHD"].ToString());

                lblTienChuaChietKhau.Text = String.Format("{0:###,###.###}", dTienChuaCK);

                //PhieuThu
                lblTenTVVPhieuThu.Text = dtHoDon.Rows[0]["TenTVV"].ToString();
                lblSoTien.Text = "";

                DataTable dt = obj.dsChiTietHoaDon(ref error, Session["MaHDPrinte"].ToString()).Tables[0];
                gvListChiTietHoaDon.DataSource = dt;
                gvListChiTietHoaDon.DataBind();
                double dTienKhuyenMaiBangTien = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["ThanhTien"].ToString() != "")
                    {
                        double dTienTemp = double.Parse(dt.Rows[i]["ThanhTien"].ToString());
                        if (dTienTemp < 0)
                        {
                            dTienKhuyenMaiBangTien = dTienKhuyenMaiBangTien + dTienTemp;
                        }
                    }
                }
                lblTienConPhaiThanhToan.Text = String.Format("{0:###,###.###}", dTienChuaCK + dTienKhuyenMaiBangTien);
                lblKhuyenMaiBangTien.Text = String.Format("{0:###,###.###}", System.Math.Abs(dTienKhuyenMaiBangTien));
                lblSoTien.Text = String.Format("{0:###,###.###}", dTienChuaCK + dTienKhuyenMaiBangTien);

                PrintHelper.PrintWebControl(pnlPrintHoaDon);
            }
            else
            {

            }
        }
        catch (Exception ex)
        {
            error = error + " Lỗi: " + ex;
            return;
        }
    }
}
